package cn.com.dhc.danlu.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;

import com.yiji.www.frameworks.utils.DensityUtils;

import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.fragment.base.BaseV4Fragment;
import cn.com.dhc.danlu.fragment.enveloplogic.EnvelopStatusPresenter;
import cn.com.dhc.danlu.fragment.enveloplogic.EnvelopeablePresenter;
import cn.com.dhc.danlu.widget.adapter.EnvelopeFragmentAdapter;
import cn.com.dhc.danlu.widget.loadlayer.LoaddingLayout;
import cn.com.dhc.mibd.eufw.widget.android.ptr.CustomPullToRefreshListView;
import cn.com.dhc.mibd.eufw.widget.android.ptr.PullToRefreshBase;
import cn.com.hd.mall.web.webservices.entity.response.cpx.GetCouponListResponse.CouponListModel;

/**
 * include :
 *
 * 未使用 已使用 已过期 fragment
 * 使用红包： 可用红包 + 不可用红包
 *
 * @author wuhaiyang
 */
public class EnvelopFragment extends BaseV4Fragment {
    private EnvelopeablePresenter mEnvelopeablePresenter;

    //    private CountCallback mListener1;
    private OnSelectEnvelop mListener2;

    //int status,int type
    public static EnvelopFragment newInstance(Bundle bundle) {
        EnvelopFragment fragment = new EnvelopFragment();
        //status 0 1 2  | 0 1
        // 0 -->代表我的优惠券  ， 1---->代表使用优惠券
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        // 由于当前fragment被使用优惠券和我的优惠券共用了 不同情况的fragment 可能需要实现不同的接口
       if (context instanceof OnSelectEnvelop) {
            mListener2 = (OnSelectEnvelop) context;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.common_listview, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        initView(getView());
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        mEnvelopeablePresenter = null;
        super.onDestroy();
    }

    public void initView(View view) {
        LoaddingLayout ll_layer = (LoaddingLayout) view.findViewById(R.id.ll_layer);
        final CustomPullToRefreshListView listview = (CustomPullToRefreshListView) view.findViewById(R.id.listview);
        Bundle bundle = getArguments();
        int status = bundle.getInt("index");
        int type = bundle.getInt("type");
        listview.getRefreshableView().setBackgroundColor(getResources().getColor(R.color.ui_bg));

        if (type == EnvelopeFragmentAdapter.TYPE_ENVELOP_STATUS) {
            listview.setMode(PullToRefreshBase.Mode.BOTH);
            listview.getRefreshableView().setDividerHeight(DensityUtils.dip2px(getActivity(), 8));
            //            EnvelopStatusPresenter presenter = new EnvelopStatusPresenter(mListener1.getDelegate(), mListener1.getDanluTaskProxy(), listview, status);
            EnvelopStatusPresenter presenter = new EnvelopStatusPresenter(getActivity(), listview, status);
            presenter.setLoaddingLayout(ll_layer);
            presenter.callFetchData();
        } else if (type == EnvelopeFragmentAdapter.TYPE_ENVELOP_USE) {
            listview.getRefreshableView().setDividerHeight(DensityUtils.dip2px(getActivity(), 8));
            listview.setMode(PullToRefreshBase.Mode.DISABLED);
            // 创建对应 可用优惠券和不可用优惠券 的listview presenter  目前数据是从上一个界面传值过来的 暂时没有用到danlutaskProxy
            //            mEnvelopeablePresenter = new EnvelopeablePresenter(mListener2.getDelegate(), listview, mListener2.getCacheJsonStr(), status);
            mEnvelopeablePresenter = new EnvelopeablePresenter(getActivity(), listview, mListener2.getCacheJsonStr(), status);
            mEnvelopeablePresenter.setLoaddingLayout(ll_layer);
            if (status == 0) {
                // 可用优惠券
                mEnvelopeablePresenter.showHistorySelect(mListener2.getLastSelect());
                listview.getRefreshableView().setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        position = position - listview.getRefreshableView().getHeaderViewsCount();
                        ImageView imageView = (ImageView) view.findViewById(R.id.iv_item);
                        CouponListModel currentSelectModel = (CouponListModel) mEnvelopeablePresenter.getModelByPos(position);
                        boolean isCheck = currentSelectModel.isChecked();
                        if (!isCheck) {
                            //选中
                            if (mListener2.isBeyondUseCouponConditions(currentSelectModel)) {
                                return;
                            }
                        }
                        imageView.setImageResource(isCheck ? R.drawable.icon_envelop_checkpress : R.drawable.icon_envelop_checknormal);
                        if (null != mListener2) {
                            mListener2.onSelect(currentSelectModel, !isCheck);
                        }
                        currentSelectModel.setChecked(!isCheck);
                    }
                });
            }
        }
    }

    /**
     * 定义该接口主要是为了通知宿主activity刷新tab数量角标
     *
     * @author wuhaiyang
     */
    //    public interface CountCallback extends OnProxyProvider {
    //    }

    public interface OnSelectEnvelop /*extends OnProxyProvider*/ {
        /**
         * @param isAdd
         *         宿主activity是添加还是移除
         */
        void onSelect(CouponListModel obj, boolean isAdd);

        /**
         * 获取 数据域 是从 订单确认界面 请求传递过来的数据
         */
        String getCacheJsonStr();

        List<CouponListModel> getLastSelect();

        boolean isBeyondUseCouponConditions(CouponListModel model);
    }

    //    interface OnProxyProvider {
    //        DanluTaskProxy getDanluTaskProxy();
    //
    //        AsyncTaskActivityDelegate getDelegate();
    //    }
}
